package com.naschain.naspay;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.naschain.naspay.nebulas.Constants;
import com.naschain.naspay.nebulas.api.SmartContracts;
import com.naschain.naspay.nebulas.model.ContractModel;
import com.naschain.naspay.nebulas.model.GoodsModel;

import java.util.Arrays;

/**
 * Created by baoyunfeng on 2018/6/9.
 */

public class NasPayManager {

    private static final String TAG = "NasPayManager";

    public static String Default_Contract_Address;

    public static void queryNasPay(final String unicode, final OnNasPayUtilListener onNasPayUtilListener) {
        SmartContracts.queryTransferStatus(Constants.MAIN_NET, unicode, new SmartContracts
                .StatusCallback() {
            @Override
            public void onSuccess(String response) {
                PayModel payModel = new Gson().fromJson(response, PayModel.class);
                if (payModel.getCode() == 0) {
                    if (onNasPayUtilListener != null) {
                        onNasPayUtilListener.success(unicode);
                    }
                } else {
                    if (onNasPayUtilListener != null) {
                        onNasPayUtilListener.fail(unicode, "购买失败，请稍后重试");
                    }
                }
            }

            @Override
            public void onFail(final String error) {
                if (onNasPayUtilListener != null) {
                    onNasPayUtilListener.fail(unicode, error);
                }
            }
        });
    }

    public static PayBuilder createPayBuilder(Context context) {
        return new PayBuilder(context);
    }

    public static class PayBuilder {

        private Context mContext;
        private String name = "NasChain";
        private String desc = "NasChain";
        private String toAddr = "n1V2fL3egjpNrDBNAzGjH3ESfqsj4nzzzoC";
        private String valueInWei = "0.0000001";
        private String unicode;


        public PayBuilder(Context context) {
            mContext = context;
        }

        public PayBuilder setName(String name) {
            this.name = name;
            return this;
        }

        public PayBuilder setDesc(String desc) {
            this.desc = desc;
            return this;
        }

        public PayBuilder setToAddr(String toAddr) {
            this.toAddr = toAddr;
            return this;
        }

        public PayBuilder setValueInWei(String valueInWei) {
            this.valueInWei = valueInWei;
            return this;
        }

        public PayBuilder setUnicode(String unicode) {
            this.unicode = unicode;
            return this;
        }

        public void build() {
            if (TextUtils.isEmpty(unicode)) {
                Log.e(TAG, "TextUtils.isEmpty(unicode)");
                return;
            }
            GoodsModel gm = new GoodsModel();
            gm.name = name;       // 商品名称
            gm.desc = desc;   //商品描述
            String toAddr = this.toAddr;  // 目标地址
            String valueInWei = this.valueInWei;    // your value
            SmartContracts.pay(mContext, Constants.MAIN_NET, gm, toAddr, valueInWei,
                    unicode);
        }
    }


    public static CallBuilder createCallBuilder(Context context) {
        return new CallBuilder(context);
    }

    public static class CallBuilder {
        private Context mContext;
        private String name = "NasChain";
        private String desc = "NasChain";
        //部署上链的，合约地址，合约地址，合约地址
        //默认Voter的地址
        private String contractAddr = Default_Contract_Address;
        private String functionName = "save";
        //参数String[] args = new String[]{"one","two","three"};
        private String[] args;
        private String valueInWei = "0.0000001";
        private String serialNumber;


        public CallBuilder(Context context) {
            mContext = context;
        }

        public CallBuilder setName(String name) {
            this.name = name;
            return this;
        }

        public CallBuilder setDesc(String desc) {
            this.desc = desc;
            return this;
        }

        public CallBuilder setValueInWei(String valueInWei) {
            this.valueInWei = valueInWei;
            return this;
        }

        public CallBuilder setContractAddr(String contractAddr) {
            this.contractAddr = contractAddr;
            return this;
        }

        public CallBuilder setFunctionName(String functionName) {
            this.functionName = functionName;
            return this;
        }

        public CallBuilder setArgs(String[] args) {
            this.args = args;
            return this;
        }

        public CallBuilder setSerialNumber(String serialNumber) {
            this.serialNumber = serialNumber;
            return this;
        }

        public void build() {
            GoodsModel goods = new GoodsModel();
            goods.name = name;
            goods.desc = desc;

            SmartContracts.call(mContext, Constants.MAIN_NET, goods, functionName, contractAddr, valueInWei, args,
                    serialNumber);
        }
    }


    public static SimulateCallBuilder createSimulateCallBuilder() {
        return new SimulateCallBuilder();
    }


    public static class SimulateCallBuilder {
        //部署上链的，合约地址，合约地址，合约地址
        //默认Voter的地址
        private String contractAddr = Default_Contract_Address;
        private String functionName = "getAll";
        //args = Arrays.toString(new Object[]{1,0,1})
        //notice: Arrays.toString() was need , if not : {"error":"json: cannot unmarshal array into Go value of type
        // string"}
        private String args;
        private OnNasSimulateCallListener onNasSimulateCallListener;

        public SimulateCallBuilder setContractAddr(String contractAddr) {
            this.contractAddr = contractAddr;
            return this;
        }

        public SimulateCallBuilder setFunctionName(String functionName) {
            this.functionName = functionName;
            return this;
        }

        public SimulateCallBuilder setArgs(String args) {
            this.args = args;
            return this;
        }

        public SimulateCallBuilder setOnNasSimulateCallListener(OnNasSimulateCallListener onNasSimulateCallListener) {
            this.onNasSimulateCallListener = onNasSimulateCallListener;
            return this;
        }

        public void build() {

            ContractModel contractModel = new ContractModel();
            contractModel.args = args;
            contractModel.function = functionName;

            String from = contractAddr;
            SmartContracts.simulateCall(contractModel, from, from, 1, new SmartContracts.StatusCallback() {
                @Override
                public void onSuccess(final String response) {
                    Log.i(TAG, "response:" + response);
                    if (onNasSimulateCallListener != null) {
                        onNasSimulateCallListener.onSuccess(response);
                    }

                }

                @Override
                public void onFail(String error) {
                    Log.i(TAG, "error:" + error);
                    if (onNasSimulateCallListener != null) {
                        onNasSimulateCallListener.onError(error);
                    }
                }
            });
        }
    }

    public interface OnNasPayUtilListener {
        void success(String unicode);

        void fail(String unicode, String msg);
    }

    public interface OnNasSimulateCallListener {
        void onSuccess(String response);

        void onError(String error);
    }
}
